package cn.lijiancn01.demo.framework.model;

import cn.lijiancn01.demo.framework.util.BeanUtil;
import com.mybatisflex.core.paginate.Page;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class PageResult<T> {
    private int total;
    private List<T> items = new ArrayList<>();

    /**
     * 将mybatis-fles 返回的分页数据转化成自定义的
     *
     * @param result
     * @param clz
     * @param <T>
     * @return
     */
    public static <T> PageResult<T> of(Page result, Class<T> clz) {
        if (result == null) {
            return new PageResult();
        }
        return new PageResult<>((int) result.getTotalPage(), BeanUtil.copyList(result.getRecords(), clz));
    }
}
